package com.basic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.business.system.Assess1Entity;
import com.business.system.Assess2Entity;
import com.business.system.AssessItemEntity;
import com.business.system.AssessJSEntity;
import com.business.system.AssessMSEntity;
import com.business.system.SystemFactory;
import com.utils.CommUtil;
import com.utils.ConnectionManager;
import com.utils.CorpTypeUtil;

public class HyflAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HyflForm hyflForm = (HyflForm) form;
		hyflForm.setOptrid(CommUtil.getUserId(request));
		List assess1Lisst = SystemFactory.getAssess1ListByHyfl(hyflForm
				.getParentid(), "0");
		saveHyfl(hyflForm);
		saveAssess1(hyflForm, assess1Lisst);
		return mapping.findForward("display");
	}

	private boolean saveAssess1(HyflForm hyflForm, List assess1Lisst) {
		boolean success = false;
		SystemFactory sf = new SystemFactory();
		for (Object obj : assess1Lisst) {
			Assess1Entity assess1 = (Assess1Entity) obj;
			assess1.setHyfl(hyflForm.getHyfl());
			List assess2List = SystemFactory.getAssess2ListByPdfl1(assess1
					.getPdfl_1());
			assess1.setPdfl_1("P1" + CommUtil.numberBuider(8));
			assess1.setOptrid(hyflForm.getOptrid());
			if (sf.addAssess1(assess1)) {
				success = true;
				for (Object obj2 : assess2List) {
					Assess2Entity assess2 = (Assess2Entity) obj2;
					List AccessItemList = SystemFactory
							.getAssessItemListByPdfl2(assess2.getPdfl_2());
					assess2.setPdfl_1(assess1.getPdfl_1());
					assess2.setPdfl_2("P2" + CommUtil.numberBuider(8));
					assess2.setOptrid(hyflForm.getOptrid());
					if (sf.addAssess2(assess2)) {
						for (Object obj3 : AccessItemList) {
							AssessItemEntity assessItem = (AssessItemEntity) obj3;
							List msList = SystemFactory.getAssessMS(assessItem
									.getPdxm());
							List jsList = SystemFactory.getAssessJS(assessItem
									.getPdxm());
							assessItem.setPdfl_2(assess2.getPdfl_2());
							assessItem.setPdxm("P3" + CommUtil.numberBuider(8));
							assessItem.setOptrid(hyflForm.getOptrid());
							if (sf.addAssessItem(assessItem)) {
								for (Object obj4 : msList) {
									AssessMSEntity assessMS = (AssessMSEntity) obj4;
									assessMS.setPdxm(assessItem.getPdxm());
									assessMS.setOptrid(hyflForm.getOptrid());
									sf.addAssessMS(assessMS);

								}
								for (Object obj5 : jsList) {
									AssessJSEntity assessJS = (AssessJSEntity) obj5;
									assessJS.setPdxm(assessItem.getPdxm());
									assessJS.setOptrid(hyflForm.getOptrid());
									sf.addAssessJS(assessJS);
								}
							}
						}
					}
				}
			}
		}
		return success;
	}

	private boolean saveHyfl(HyflForm hyflForm) {
		Connection con = null;
		PreparedStatement pstmt = null;
		boolean success = false;
		try {
			con = ConnectionManager.getConnection();
			String strSql = "select top 1 * from sys_hyfl order by hyfl desc";
			pstmt = con.prepareStatement(strSql);
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				String yhValue = rs.getString("hyfl");
				System.out.println(yhValue);
				hyflForm.setHyfl((Integer.parseInt(yhValue) + 1) + "");
			}

			strSql = "insert into sys_hyfl(hyfl,hyflmc,optrid,optdate,parentid) values (?,?,?,GETDATE(),?)";
			pstmt = con.prepareStatement(strSql);
			pstmt.setString(1, hyflForm.getHyfl()); // hyfl
			pstmt.setString(2, hyflForm.getHyflmc()); // pdfl_1
			pstmt.setString(3, hyflForm.getOptrid()); // pdflmc_1
			pstmt.setString(4, hyflForm.getParentid()); // bzf
			success = pstmt.execute();
			CorpTypeUtil.corpTypeMap.put(hyflForm.getHyfl(), hyflForm
					.getHyflmc());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				pstmt.close();
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return success;

	}
}
